package apis

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/base64"
	"encoding/pem"
	"log"
	"os"
)

var (
	Private1 = []byte(`-----BEGIN private-----
MIICXQIBAAKBgQC32pYyKWAWiI4CWZzxLgpad2pz1r6F9opDJaNQvFTQJiQivHlh
sHQvyZ90ZDZbDxxAX/sqK4HMkDfBPBmol4UHRg5kAv9xkVEmyBtoLLljYRszxOje
693EyKDAeME8KNVgLLaCsMuiyy2EaZMBGAvxSTmnd+t1pMjHHp8n7DGunwIDAQAB
AoGBAJf9oftYDzKxs0yoDsGnwTFm1V9sYVdYKJc4L1pYR3lIfskkrOgguvyinyrW
icUfqchiUL5c91JIy375E3E3yeV/xoS4y+6Cf0dAiq/iiGoBqBtzbSpF+UA0o/pP
3yMu8WaueG2En8mpTN9kAcW9Cn279PtwMALfKmW1/NZdXJ2pAkEA8RL0gXGukWOZ
auEMxp0OWvQm0R/lVz2XyfjSVQWfDT2vGMgDv6+2bUeT8DomJmx3oWQX2gq0uidb
/Ir9rEvhWwJBAMM8r8GVva1gicTswBWi7tDHC8OGYY9a0MQ3IPCKDCC513Se0SJi
rhSU1IL2PBBgnSS/atwJ+FwY6Cbynrs1Rw0CQQDBKJ3upcboyXQgtgfFkAvzhpMf
lr3s4/YeJ5dn9mQVeOo2IfUW/tRdxS9dGUp+GQTVtNyDoXZjfavvsvJEvlvnAkAJ
h6nQmj2S+/ZdnWzW4YgkusKRPbr/Y2BzY3mBJEvpQQkKUUJGGmkC6izhY6GU7xJc
m9dcfEQaQsD6PbePzc71AkBEanolW+cy+XzlqxQPoR64h9oP+QaVS0mlD+u3YBWn
M0OVEU2E6lZ2xR25WmM/VAplMzklBlfIhsfkvBDHFUDX
-----END private-----`)
	Public1 = []byte(`-----BEGIN public-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC32pYyKWAWiI4CWZzxLgpad2pz
1r6F9opDJaNQvFTQJiQivHlhsHQvyZ90ZDZbDxxAX/sqK4H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1frG
r9alMUh6StKF2dc3EOrvYzIZ48NVdAD6GK+Loyg+ZMhDFNNQnin89yhN6E9A+Qnr
IMJUU52XpHorWhbMYjGK/Dc9s8wkGKguDdW0oKxqsRsJpUBMZD0pWmYL1zs=
-----END private-----`)
	Public = []byte(`-----BEGIN public-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAy/RAwMmATteAPZH4Ev3h
7wR0qPXhDWQrqd5MYker4V0V3L3kcF8CNFnrEPznHVGbVhXGV5ulI41hvbPAjjR+
Z94PWvfXbyVMWhOYXsaiogSlwXrFDx+pVsACBO4bBZLU6HqrwbDq6HI+N4AmqXF7
6GhkO7t12hLoq/9fr5lCIWeBCFBHP9pGlt6rfIgMNPDb+rrsVmKgC2dkNnqK15+T
cBX7CBy7OS9iFduOexTUuXsZLmEc/Vpsjja7wJRky4aPMYI1JUypdKZAx2VdHX1t
+20IB2zA8JleR4Rx6E326CsELLySBrcwBEJ0Uuv7ti8Q2k//0+5rjCGb4MVgjF9e
4AhKqG7z0iORqO/vMRlah8ka7uUtWuKKm6oRrc3EDOkdVuF80P2PAJ7RnNGh1rg0
C16DHFplKevKC+6qHrRelbD3Ct4i+axBGPdZGDWF1xV/2lkFtxfcM5GHonEbXROm
bH7pe4xEqR0qQzC1keu3bmpXR71fc0uUgS8HsT61fpxiGXnmsTGJkJh7GlYo/3+W
hELaAqAdvRJcdsJS72b7bDXaouqDpS9NOoaBXLN8OqrZ6ee9gJ2rfiMllkjDYke9
KCJ0qyOvYoecw6lZcAV9JYmbqNbwYRY4TejOEEYnoF1QZKntowz/oKbel3BV4md3
WV0w/3u7nUcgzmZNkJ6aZ08CAwEAAQ==
-----END public-----`)
)

func initrsa() {
	//GenRsaKey(1024)
	//	str := RsaEncrypt("你好")
	//	log.Debug(str)
	//	log.Debug(RsaDecrypt(str))
}

func RsaEncrypt(orig string) (encodestr string) {
	origData := []byte(orig)
	block, _ := pem.Decode(Public)
	if block == nil {
		log.Println("public key error")
		return
	}
	pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
	if err != nil {
		return
	}
	pub := pubInterface.(*rsa.PublicKey)
	res, err := rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
	if err == nil {
		encodestr = base64.StdEncoding.EncodeToString(res)
	} else {
		log.Println("rsa en err", err.Error())
	}
	return
}

// 解密
func RsaDecrypt(text string) (orig string) {
	ciphertext, err := base64.StdEncoding.DecodeString(text)
	if err != nil {
		log.Println("ras De error", err.Error())
		return
	}
	block, _ := pem.Decode(Private)
	if block == nil {
		log.Println("private key error!")
		return
	}
	priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
	if err != nil {
		return
	}
	res, err := rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
	if err == nil {
		orig = string(res)
	} else {
		log.Println("ras De error", err.Error())
	}
	return
}

func GenRsaKey(bits int) error {
	// 生成私钥文件
	privateKey, err := rsa.GenerateKey(rand.Reader, bits)
	if err != nil {
		return err
	}
	derStream := x509.MarshalPKCS1PrivateKey(privateKey)
	block := &pem.Block{
		Type:  "private",
		Bytes: derStream,
	}
	file, err := os.Create("private.pem")
	if err != nil {
		return err
	}
	err = pem.Encode(file, block)
	if err != nil {
		return err
	}
	// 生成公钥文件
	publicKey := &privateKey.PublicKey
	derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
	if err != nil {
		return err
	}
	block = &pem.Block{
		Type:  "public",
		Bytes: derPkix,
	}
	file, err = os.Create("public.pem")
	if err != nil {
		return err
	}
	err = pem.Encode(file, block)
	if err != nil {
		return err
	}
	return nil
}
